using Microsoft.EntityFrameworkCore;
using Project_Schedule.Configures;
using Project_Schedule.Domain.BackGround.com.core.Interface;
using Project_Schedule.Domain.Request;
using Project_Schedule.Model;
using Project_Schedule.Model.EntitiesHelper;

namespace Project_Schedule.Domain.BackGround.com.core.Service;

public class Bg_StartUsingService : IBackgroundStartUsing
{
    public async Task<JRequest> WebUserIsUsing(string account)
    {
        try
        {
            await using var db = new DataDbContext();
            var info = await db.SystemUsers?.FirstOrDefaultAsync(e =>
                e.Account == account);
            if (info is null) throw new Exception("用户信息错误");
            if (info.IsUsed == System_Audit_Status_Enum.Using) throw new Exception("用户已经启用");
            info.IsUsed = System_Audit_Status_Enum.Using;
            await db.SaveChangesAsync();
            return JRequest.Ok(true);
        }
        catch (Exception exception)
        {
            _logHelper.LogError(exception);
            return JRequest.Ok(false, false, exception.Message);
        }
    }

    private readonly INLogHelper _logHelper;

    public Bg_StartUsingService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }
}